package com.qfmy.webapp.service.impl.common;

import com.qfmy.webapp.mapper.common.HotSearchRankMapper;
import com.qfmy.webapp.service.common.HotSearchRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.novel
 * @date 2025/11/02
 * @description 热搜
 */
@Service
public class HotSearchRankServiceImpl implements HotSearchRankService {

    /**
     * 注入mapper
     */
    @Autowired
    private HotSearchRankMapper hotSearchRankMapper;

    /**
     * 注入redis
     */
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取热搜
     * @return
     */
    @Override
    public List<String> getHotSearchRank() {
        //创建缓存key
        String key = "hotSearchTag";
        //判断缓存是否存在
        if(redisTemplate.hasKey(key))
        {
            return (List<String>) redisTemplate.opsForValue().get(key);
        }
        //缓存不存在,查询数据库
        List<String> hotSearchRank = hotSearchRankMapper.getHotSearchRank();
       //创建ttl
        Long ttl = hotSearchRank.isEmpty() ? 1 : 6L;
        //缓存数据
        redisTemplate.opsForValue().set(key, hotSearchRank, ttl, TimeUnit.HOURS);
        //返回数据
        return hotSearchRank;
    }
}
